//
//  RevampPhoneViewController.m
//  Village
//
//  Created by longrise on 2017/9/21.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "RevampPhoneViewController.h"
#import "RevampTableViewCell.h"
#import "RevampTwoViewController.h"
@interface RevampPhoneViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
    NSInteger selectIndex;
}
@property (nonatomic , strong) UITableView *mytableview;
@property (nonatomic ,strong)UIImageView *Pimage;
@property (nonatomic ,strong) NSMutableArray *leftary;
@property (nonatomic ,strong) NSMutableArray *tishiary;
@property (nonatomic ,strong) UIButton *surebutton;
@property (nonatomic ,strong) UILabel *zhucelable;
@property (nonatomic ,strong) NSString *stra;
@property (nonatomic ,strong) NSString *strb;
@property (nonatomic ,strong) NSString *strc;
@property (nonatomic ,strong) NSString *strd;
@property (nonatomic ,strong) NSString *strf;
@property (nonatomic ,strong) NSString *poochange;
@property (nonatomic ,strong) NSString *tf;
@property (nonatomic ,strong) NSString *style;// 1 注册 2 忘记密码
@property (nonatomic, copy) LongriseRouterBlock routerBlock;
@end

@implementation RevampPhoneViewController

- (void)setParams:(id)obj block:(LongriseRouterBlock)block
{
    self.style = (NSString *)obj;
    self.routerBlock = block;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidDisappear:(BOOL)animated{
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackgroundColor;
    
    self.title = @"修改手机号";
        _tishiary =[[NSMutableArray alloc] initWithObjects:@"请输入手机号",@"请输入图片验证码",@"请输入短信验证码" ,nil];
    [self Buildtheforgetpage];
       [self gaindata];
}

- (void)Buildtheforgetpage{
    _mytableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, KDeviceHeight) style:UITableViewStylePlain];
    _mytableview.delegate = self;
    _mytableview.dataSource = self;
    _mytableview.scrollEnabled =NO; //设置tableview 不能滚动
    [self.view addSubview:_mytableview];
    
    //    _pooCodeView = [[PooCodeView alloc] initWithFrame:CGRectMake(267*kScreenScaleX, 107.5*kScreenScaleY, 96*kScreenScaleX, 35*kScreenScaleY) andChangeArray:nil];
    //    //注意:文字高度不能大于poocodeview高度,否则crash
    //    _pooCodeView.textSize = 25*kScreenScaleY;
    //    _pooCodeView.backgroundColor = K_Yanzhengma;
    //    [_mytableview addSubview:_pooCodeView];
    
    _Pimage = [[UIImageView alloc] initWithFrame:CGRectMake(267*kScreenScaleX, 57.5*kScreenScaleY, 96*kScreenScaleX, 35*kScreenScaleY)];
    //    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    
    
//    NSURL *url  = [[NSURL alloc] initWithString:IMGCodeURL];
    //    [_Pimage sd_setImageWithURL:url];
    [self tapAvatarView];

    [_mytableview addSubview:_Pimage];
    
    _Pimage.userInteractionEnabled = YES;
    UITapGestureRecognizer * PrivateLetterTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAvatarView)];
    PrivateLetterTap.numberOfTouchesRequired = 1; //手指数
    PrivateLetterTap.numberOfTapsRequired = 1; //tap次数
    _Pimage.contentMode = UIViewContentModeScaleToFill;
    [_Pimage addGestureRecognizer:PrivateLetterTap];
    
    
    
    UIView *footview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KDeviceHeight, 600*kScreenScaleY)];
    footview.backgroundColor = kBackgroundColor;
    _mytableview.tableFooterView = footview;
    
    _surebutton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_surebutton setTitle:@"下一步" forState:UIControlStateNormal];
    _surebutton.tintColor = [UIColor whiteColor];
    _surebutton.layer.cornerRadius = 5.0*kScreenScaleX;//2.0是圆角的弧度，根据需求自己更改
    _surebutton.titleLabel.font = [UIFont systemFontOfSize:18*kScreenScaleX];
    _surebutton.frame = CGRectMake(30*kScreenScaleX, 20*kScreenScaleY, 315*kScreenScaleX, 45*kScreenScaleY);
    //    [_surebutton setBackgroundImage:[UIImage imageNamed:@"BG"]forState:UIControlStateNormal];
    _surebutton.backgroundColor = K_Anniuhong;
    [_surebutton addTarget:self action:@selector(sureaction) forControlEvents:UIControlEventTouchUpInside];
    [footview addSubview:_surebutton];
    
    
    _zhucelable = [[UILabel alloc] initWithFrame:CGRectMake(116*kScreenScaleX, 10*kScreenScaleY, 250, 30*kScreenScaleY)];
    _zhucelable.text = [Globle getInstance].account.phone;
    _zhucelable.textColor = [UIColor blackColor];
    [_zhucelable setTextAlignment : NSTextAlignmentLeft] ;
    _zhucelable.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:_zhucelable];

}

#pragma mark - 数据源方法

- (void)gaindata{
    _stra = [[NSString alloc] init];
    _strb = [[NSString alloc] init];
    _leftary =[[NSMutableArray alloc] initWithObjects:@"原手机号码",@"验证码",@"短信验证码" ,nil];
    
}
// 返回多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
// 返回行数
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
}
// 设置cell
- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    static NSString *cellID = @"id";
    RevampTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil)
    {
        cell = [[RevampTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.backgroundColor = [UIColor whiteColor];
    }
    
    if (indexPath.row == 0 ){
        cell.gainbutton.hidden = YES;
        cell.seebutton.hidden = YES;
        cell.textfield.hidden = YES;
        
    }else  if (indexPath.row == 1 ){
        cell.gainbutton.hidden = YES;
        cell.textfield.secureTextEntry = NO;
         cell.seebutton.hidden = YES;
    }else{
        cell.textfield.keyboardType = UIKeyboardTypeNumberPad;
        cell.seebutton.hidden = YES;
        cell.gainbutton.hidden = NO;
        cell.gainClickBlock = ^{
            [self getMSGCode];
        };
    }
    cell.textfield.tag = indexPath.row;
    cell.textfield.delegate = self;
    [cell.textfield addTarget:self action:@selector(textFieldWithText:) forControlEvents:UIControlEventEditingChanged];
    cell.leftlable.text = _leftary[indexPath.row];
    cell.textfield.placeholder = _tishiary[indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];//cell点击之后没有视觉效果
    
    return cell;
}

- (void)textFieldWithText:(UITextField *)textField
{
    switch (textField.tag) {
        case 0:
            self.stra = textField.text;
            break;
        case 1:
            self.strb = textField.text;
            break;
        case 2:
            self.strc = textField.text;
            break;
        default:
            break;
    }
}

- (void)tapAvatarView{
    
    _Pimage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:IMGCodeURL]]];
}
#pragma mark - 代理方法
//设置行高
- (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return 50*kScreenScaleY;
}

#pragma mark - 提交方法
- (void)sureaction{
  
    
    if (!self.strc.length) {
        [MBProgressHUD showError:@"请输入短信验证码！" toView:self.navigationController.view];
    }
    else{
       BOOL isResult = [_strc compare:_tf];
        if (isResult == 0){
            id obj = [LongriseRouter openURL:@"RevampTwoViewController"];
            if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
            {
                id<LongriseRouterDelegater> delegater = obj;
                if(delegater)
                {
                    //进行传值，传入统一的block以供B进行回调
                    [delegater setParams:@"1" block:^(id obj) {
                        
                        NSLog(@"回调数据：%@",obj);
                    }];
                }
            }
            if (obj && [obj isKindOfClass:[UIViewController class]]) {
                UIViewController *VC = obj;
                VC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:VC animated:YES];
            }
        }else{
             [MBProgressHUD showError:@"请输入正确的短信验证码！" toView:self.navigationController.view];
        }
        
    }
}

//- (void)matchAccountInfo
//{
//    NSMutableDictionary *bean = [NSMutableDictionary dictionary];
//    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//    [bean setValue:dict forKey:@"entitybean"];
//    [dict setObject:[Globle getInstance].account.userid forKey:@"id"];
//    [dict setObject:[Globle getInstance].account.account forKey:@"userflag"];
//    [dict setObject:[Globle getInstance].account.phone forKey:@"phone"];
//
//    WeakSelf(weakSelf);
//    [NetRequestClass Post:MatchAccountInfoURL Parameters:bean success:^(id result) {
//        [MBProgressHUD hideHUDForView:weakSelf.navigationController.view];
//        [MBProgressHUD showSuccess:result[@"message"] toView:weakSelf.navigationController.view Completion:^{
//            if (weakSelf.routerBlock) {
//                weakSelf.routerBlock([Globle getInstance].account);
//                [weakSelf.navigationController popViewControllerAnimated:YES];
//            }
//        }];
//
//    } failure:^(id valueCode, id error) {
//        [MBProgressHUD hideHUDForView:weakSelf.navigationController.view];
//        if (weakSelf.routerBlock) {
//            weakSelf.routerBlock([Globle getInstance].account);
//            [weakSelf.navigationController popViewControllerAnimated:YES];
//        }
//    }];
//}
#pragma mark - 获取短信验证码
- (void)getMSGCode
{
    if (!self.strb.length) {
        [MBProgressHUD showError:@"请输入图片验证码！" toView:self.navigationController.view];
    }else{
     
        NSMutableDictionary *bean = [NSMutableDictionary dictionary];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:[Globle getInstance].account.phone forKey:@"phone"];
        [dict setObject:self.strb forKey:@"imagecode"];
        [bean setValue:dict forKey:@"entitybean"];
        
        [NetRequestClass Post:RegisterGetMSGCodeURL Parameters:bean success:^(id result) {
            [MBProgressHUD hideHUDForView:self.navigationController.view];
         
//            [MBProgressHUD showSuccess:result[@"message"] toView:self.navigationController.view Completion:nil];
            [MBProgressHUD showError:result[@"data"] toView:self.navigationController.view];
           
            if ([result[@"message"] isEqualToString:@"成功"]){

                [[NSNotificationCenter defaultCenter] postNotificationName:@"Second" object:self userInfo:nil];
            }
            LSLog(@"msg -----  %@",result);
            _tf = result[@"data"];
        } failure:^(id valueCode, id error) {
            [MBProgressHUD hideHUDForView:self.navigationController.view];
            [MBProgressHUD showError:error toView:self.navigationController.view];
        }];
    }
}


@end
